Can't append to array using string field name [$] when performing update on array fields

Posted by Haraldo on Stack Overflow See other posts from Stack Overflow or by Haraldo
Published on 2012-10-23T22:31:15Z Indexed on 2012/10/23 23:00 UTC
Read the original article Hit count: 239

Filed under:
|

rowsI am attempting to perform a mongodb update on each field in an array of records.

An example schema is below:

{
    "_id" : ObjectId("508710f16dc636ec07000022"),
    "summary" : "",
    "uid" : "ABCDEF",
    "username" : "bigcheese",
    "name" : "Name of this document",
    "status_id" : 0,
    "rows" : [
        {
            "score" : 12,
            "status_id" : 0,
            "uid" : 1
        },
        {
            "score" : 51,
            "status_id" : 0,
            "uid" : 2
        }
    ]
}

So far I have been able to perform single updates like this:

db.mycollection.update({"uid":"ABCDEF","rows.uid":1}, {$set:{"rows.$.status_id":1}},false,false)

However, I am struggling as to how to perform an update that will update all array records to a status_id of 1 (for instance).

Below is how I imagine it should work:

db.mycollection.update({"uid":"ABCDEF"}, {$set:{"rows.$.status_id":1}},false,true)

However I get the error:

can't append to array using string field name [$]

I have tried for quite a while with no luck. Any pointers?

© Stack Overflow or respective owner

Related posts about mongodb

Related posts about mongodb-update